#include <stdio.h>
#include <stdlib.h>

/** Biref Description
 * Description
 * @param 
 * @see 
 * @return 
 */

int gcd (int a, int b)
{
    int t;

    while (b != 0) {
        t = a % b;
        a = b;
        b = t;
    }

    return a;
}

int main (int argc, char const* argv[])
{
    int n, a, b, c;

    scanf("%d", &n);
    while (n--) {
        scanf("%d %d", &a, &b);

        c = 2 * b;
        while (gcd(a, c) != b) {
            c = c + b;
        }

        printf("%d\n", c);
    }

    return 0;
}
